package com.songtech.device.service.impl;

import com.songtech.device.bean.Device;
import com.songtech.device.dao.DeviceMapper;
import com.songtech.device.service.DeviceService;
import com.songtech.product.bean.Product;
import com.songtech.product.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @Author: yangzhicheng
 * @Date: 2018/6/20 10:02
 */
@Service
public class DeviceServiceImpl implements DeviceService {

    Logger logger = LoggerFactory.getLogger(DeviceServiceImpl.class);
    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private ProductService productService;

    /**
     * 记录设备上线或下线
     * @param imei 设备号
     * @param status 状态 0上线，1下线
     */
    @Override
    public void setDevice(String imei,Integer status) {
        try {
            Product product = productService.getProductByImei(imei);
            Device device = new Device();
            if(product != null){
                device.setCompanyCode(product.getCompanyCode());
                device.setProductCode(product.getProductCode());
                device.setModelNumber(product.getModelNumber());
            }
            device.setImei(imei);
            device.setCreateTime(new Date());
            device.setStatus(status);
            deviceMapper.insert(device);
        }catch(Exception e){
            logger.error("同步设备上下线异常: "+e.getMessage(),e);
        }

    }

    @Override
    public int deviceOnlineCount(Map<String, String> map) {
        return deviceMapper.deviceOnlineCount(map);
    }

    @Override
    public int deviceOfflineCount(Map<String, String> map) {
        return deviceMapper.deviceOfflineCount(map);
    }

    /**
     *根据天数查询设备在线数据
     * @param map
     * @return
     */
    public List<Device> deviceOnlineByDay(Map<String, Object> map) {
        return deviceMapper.deviceOnlineByDay(map);
    }
}
